<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Api\ApiController;
use App\Http\Forms\UnlockGoodForm;
use App\Modules\Free\Models\FreeRecord;
use App\Modules\Free\Models\UnlockGood;
use App\Modules\Free\Models\UnlockRecord;
use App\Modules\Good\Models\Good;
use App\Modules\Package\Models\GoodPackage;
use App\Modules\Shop\Manager;
use App\Modules\Shop\Shop;
use App\Modules\User\Models\User;
use Illuminate\Http\Request;

class FreeController extends ApiController
{
    public function index(Request $request)
    {
        $name = $request->input('name');
        $goods = $request->input('goods');

        $shop = Manager::getShop();
        $query = FreeRecord::query()
            ->join('users', 'users.id', '=', 'free_records.user_id')
            ->select('users.nickname', 'users.avatar', 'free_records.*');
        if (!empty($name)) {
            $query->where('nickname', 'like', '%'.$name."%");
        }

        if (!empty($goods)) {
            $query->where('title', 'like', '%'.$goods."%");
        }
        $data = $query->paginate(20);
        $sidebar = 'record';
        return view('free.index', compact('data', 'name', 'goods', 'shop', 'sidebar'));
    }

    public function unlockList($id, Request $request)
    {
        $free = FreeRecord::query()->find($id);
        if (is_null($free)) {
            return redirect()->back()->withErrors("参数错误");
        }
        $unlock = UnlockRecord::query()
            ->where('free_record_id', $free->id)
            ->get();
        $user = User::query()->find($unlock->pluck('buyer_id')->all())->keyBy('id');
        $sidebar = 'record';
        return view('free.unlock', compact('sidebar', 'free', 'unlock', 'user'));
    }

    public function cancelUnlock($id)
    {
        $record = UnlockRecord::query()
            ->where('id', $id)
            ->where('status', UnlockRecord::STATUS_INIT)
            ->first();

        if (!is_null($record)) {
            $record->update(['status' => UnlockRecord::STATUS_FAIL]);
            FreeRecord::query()
                ->find($record->free_record_id)
                ->decrement('unlocked', $record->money);
        }
        return redirect()->back();
    }

    public function passUnlock($id)
    {
        $record = UnlockRecord::query()
            ->where('id', $id)
            ->where('status', UnlockRecord::STATUS_INIT)
            ->first();
        if (is_null($record)) {
            return redirect()->back()->withErrors("状态错误");
        }
        $record->update([
            'status' => UnlockRecord::STATUS_ISSUED,
        ]);
        /** @var User $user */
        $user = User::query()
            ->where('id', $record->user_id)
            ->first();
        $account = $user->getAccount();
        $account->incrementMoney($record->money, 'system',  "解锁");
        return redirect()->back();
    }

    public function getGood(Request $request)
    {
        $type = $request->input('type', 'good');

        $shop = Manager::getShop();
        if ($type == 'package') {
            $data = GoodPackage::query()
                ->where('is_free', 1)
                ->paginate(20);
        } else {
            $data = Good::query()
                ->where('is_free', 1)
                ->paginate(20);
        }
        $sidebar = 'good';
        return view('free.good', compact('data', 'type', 'shop', 'sidebar'));
    }

    public function cancel($id, Request $request)
    {
        $type = $request->input('type');
        if ($type == 'package') {
            GoodPackage::query()
                ->where('id', $id)
                ->update([
                    'is_free' => 0,
                ]);
        } else {
            Good::query()
                ->where('id', $id)
                ->update([
                    'is_free' => 0,
                ]);
        }
        return redirect()->back();
    }


    public function getUnlockGood($id, Request $request)
    {
        $type = $request->input('type');
        $shop = Manager::getShop();
        if ($type == 'package') {
            $good = GoodPackage::query()
                ->find($id);
        } else {
            $good = Good::query()
                ->find($id);
        }
        $data = UnlockGood::query()
            ->where('free_good_type', $type)
            ->where('free_good_id', $id)
            ->get();
        foreach ($data as $value) {
            if ($value->unlock_good_type == 'package') {
                $tmp = GoodPackage::query()
                    ->find($value->unlock_good_id);
            } else {
                $tmp = Good::query()
                    ->find($value->unlock_good_id);
            }
            $value->category = $tmp->category;
            $value->name = $tmp->name;
            $value->shop = $shop->get($tmp->shop_id)->name;
        }
        $sidebar = 'good';
        return view('free.unlock_good', compact('data', 'good', 'id', 'type', 'shop', 'sidebar', 'type'));
    }

    public function createUnlockGood($id, Request $request)
    {
        $type = $request->input('type');

        $form = $this->form(UnlockGoodForm::class, [
            'method' => 'PUT',
            'url' => route('admin.free.unlock_good.store', ['id' => $id, 'free_type' => $type]),
        ]);
        $sidebar = 'good';
        return view('free.form', compact('form', 'sidebar'));
    }

    public function storeUnlockGood($id, Request $request)
    {
        $type = $request->input('free_type');
        $form = $this->form(UnlockGoodForm::class);
        if (!$form->isValid()) {
            return redirect()->back()->withErrors($form->getErrors())->withInput();
        }
        $data = $form->getFieldValues();
        $data['money'] *= 100;
        if ($type == "package") {
            $good = GoodPackage::query()->find($id);
        } else {
            $good = Good::query()->find($id);
        }
        UnlockGood::query()
            ->updateOrCreate([
                'shop_id' => $good->shop_id,
                'free_good_type' => $type,
                'free_good_id' => $id,
                'unlock_good_type' => $data['type'],
                'unlock_good_id' => $data['unlock_good_id']
            ], [
                'money' => $data['money']
            ]);
        return redirect()->route('admin.free.unlock_good', ['id' => $id, 'type' => $type]);
    }

    public function delUnlockGood($id)
    {
        UnlockGood::query()
            ->where('id', $id)
            ->delete();
        return redirect()->back();
    }
}
